package com.atguigu;

import com.alibaba.cloud.nacos.NacosConfigManager;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.Listener;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

@EnableFeignClients
@SpringBootApplication
public class orderApplication {
    public static void main(String[] args) {
        SpringApplication.run(orderApplication.class,args);
    }

    // 1. 项目启动就监听配置中心变化
    // 2. 发生变化拿到变化的值
    // 3. 发送邮件
    @Bean
    ApplicationRunner applicationRunner(NacosConfigManager nacosConfigManager){
       return args -> {
           System.out.println("项目启动就监听配置中心变化");
           ConfigService configService = nacosConfigManager.getConfigService();
           configService.addListener("order.properties", "DEFAULT_GROUP", new Listener() {
               @Override
               // 监听器在线程池中运行
               public Executor getExecutor() {
                   return Executors.newFixedThreadPool(4);
               }

               @Override
               // 接受配置信息
               public void receiveConfigInfo(String configInfo) {
                   System.out.println("变化的配置信息" + configInfo);
                   System.out.println("发送邮件");
               }
           });
       };
    };
}
